<?php

/**
 * @copyright   FOSP
 * @version     20120412
 * @todo        Rating Player Statistic model
 * @name        RatingPlayerStatistic
 * @author      QuangTM 
 */
class RatingPlayerStatistic
{

    const STATISTIC_TABLE = 'statistic_table';

    /**
     *
     * @var RatingPlayerStatistic
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get RatingPlayerStatistic's instance
     * @return RatingPlayerStatistic
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get statistic table
     * @param int $matchID
     * @return array|boolean 
     * @author QuangTM
     */
    public function getStatisticTable($matchID)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($matchID);
        $keyCache         = Fpt_Global::makeCacheKey(self::STATISTIC_TABLE, array($matchID));
        // Get statistic table from cache
        $statisticTable = $memcacheInstance->read($keyCache);

        // If miss cache
        if ($statisticTable === FALSE)
        {
            try
            {
                // Get application's configuation
                $config = Fpt_Global::getApplicationIni();

                // Get obj to access table rating_player
                $statisticMysqlObj = Fpt_Business_RatingPlayerStatistic::factory($config['database']['default']);
                
                // Read from MySQL
                $statisticTable = $statisticMysqlObj->getRatingPlayerStatistic($matchID);
                
                // If have data
                if (count($statisticTable))
                    Fpt_Global::writeMemcache ($matchID, $keyCache, $statisticTable);
                else
                {
                    $statisticTable = FALSE;
                    Fpt_Global::writeMemcache ($matchID, $keyCache, -1);
                }
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }

        return $statisticTable;
    }

    /**
     * Insert/update into DB and precache for statistic table
     * @param array $arrStatistic
     * @param int $matchID
     * @return boolean 
     * @author QuangTM
     */
    public function modifyStatisticTable($arrStatistic, $matchID)
    {
        try
        {
            // Get format instance for statistic
            $formatInstance = new Fpt_Business_RatingPlayerStatistic_Metadata_RatingPlayerStatistic();
            
            // Get application's configuation
            $config = Fpt_Global::getApplicationIni();
            
            // Get object mysql
            $statisticMysqlObj = Fpt_Business_RatingPlayerStatistic::factory($config['database']['default']);
            
            // Get statisticTable first
            $statisticTable = $this->getStatisticTable($matchID);
            
            // If dont have statistic table
            if ($statisticTable === FALSE || $statisticTable == -1)
            {
                $statisticTable = array();
                // Loop to insert into DB and precache
                foreach ($arrStatistic as $statistic)
                {
                    $beautiFormat = $formatInstance->init($statistic)->getFormatedData();
                    // Insert into DB
                    $statisticMysqlObj->insertRatingPlayerStatistic($beautiFormat);
                    // Precache
                    $statisticTable[$beautiFormat['PlayerID']] = $beautiFormat;
                }
            }
            else
            {
                foreach ($arrStatistic as $statistic)
                {
                    $beautiFormat = $formatInstance->init($statistic)->getFormatedData();
                    // Insert into DB
                    $statisticMysqlObj->insertRatingPlayerStatistic($beautiFormat);
                    // Precache
                    $statisticTable[$beautiFormat['PlayerID']]['TotalUserID']++;
                    $statisticTable[$beautiFormat['PlayerID']]['RatingUser'] += $beautiFormat['RatingUser'];
                    $statisticTable[$beautiFormat['PlayerID']]['AvgRating'] = number_format(($statisticTable[$beautiFormat['PlayerID']]['RatingUser'] + $statisticTable[$beautiFormat['PlayerID']]['RatingVne']) / ($statisticTable[$beautiFormat['TotalUserID']]['TotalUserID'] + 1), 2);
                    $statisticTable[$beautiFormat['PlayerID']]['AvgUser'] = number_format($statisticTable[$beautiFormat['PlayerID']]['RatingUser'] / $statisticTable[$beautiFormat['TotalUserID']]['TotalUserID'], 2);
                }
            }
            
            // Write to cache
            $keyCache = Fpt_Global::makeCacheKey(self::STATISTIC_TABLE, array($matchID));
            Fpt_Global::writeMemcache($matchID, $keyCache, $statisticTable);
            return TRUE;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}